package cn.livingCloud.service.impl;

import cn.livingCloud.dto.ReplySetFormDto;
import cn.livingCloud.dto.ReplySetListDto;
import cn.livingCloud.entity.core.ReplySet;
import cn.livingCloud.repository.ReplySetMapper;
import cn.livingCloud.service.ReplySetService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 唐旭 on 2016/6/1.
 */
@Service
public class ReplySetServiceImpl implements ReplySetService {

    @Autowired
    private ReplySetMapper mapper;

    @Override
    public void loadList(ReplySetListDto listDto) {
        listDto.addAllList(mapper.getList(listDto));
    }

    @Override
    public void delete(String uuid) {
        mapper.deleteByPrimaryKey(uuid);
    }

    @Override
    public ReplySetFormDto loadForm(String uuid) {
        if (StringUtils.isNotEmpty(uuid)) {
            ReplySet set = mapper.selectByPrimaryKey(uuid);
            return new ReplySetFormDto(set);
        }
        return new ReplySetFormDto();
    }

    @Override
    public void updateReplySet(ReplySetFormDto formDto) {
        String uuid = formDto.getUuid();
        ReplySet set;
        if (StringUtils.isNotEmpty(uuid)) {
            set = mapper.selectByPrimaryKey(uuid);
            formDto.update(set);
            mapper.updateByPrimaryKeyWithBLOBs(set);
        } else {
            set = new ReplySet();
            formDto.update(set);
            mapper.insert(set);
        }
    }
}
